(function () {
    init();//唯一的执行入口
    function init() {
        window.baseUrlVar="/ruofeng/app/privilege/resource";
        window.pagePublicParam = window.ruofeng.param;
        initVue();
        axiosData();
    }

    //远程数据初始化界面
    function axiosData() {
        if (typeof (pagePublicParam.id)==="undefined") {
        } else {
            var url = `${window.baseUrlVar}?id=${pagePublicParam.id}`
            axios.get(url).then(function (response) {
                window.menuInfo.$data.form = response.data.data[0];
            }).catch(function (error) {
                console.log(error);
            })
        }
    }

    //vue绑定数据模型
    function initVue() {
        var config = {
            el: '#app',
            data() {
                return {
                    form: {
                        name: '节点名称',
                        id: pagePublicParam.id,
                        parentId: pagePublicParam.parentId,
                        icon: "",
                        category:"menu",
                        description: "",
                        permission: "",
                        status: "1",
                        url: '#'
                    }
                }
            },
            methods: getMethods()
        }
        window.menuInfo = new Vue(config)
    }

    //数据模型的事件部分
    function getMethods() {
        return {
            onSubmit() {
                var urlVar = `${baseUrlVar}`;
                var form = window.menuInfo.$data.form;
                var methodType;
                if (typeof (pagePublicParam.id)==="undefined") {
                    methodType = "post";
                } else {
                    methodType = "put";
                }
                axios({
                    method: methodType,
                    url: urlVar,
                    data: JSON.stringify(form),
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }).then(response => response.data)
                    .then(data => {
                        window.menuInfo.$message({
                            type: 'info',
                            message:data.message
                        });
                    });
            },
            onCancel() {
                parent.vue.$data.activeName = "first";
            }
        }
    }
})()